;
! function() {
	var $ = layui.jquery,
		layer = layui.layer,
		form = layui.form,
		carousel = layui.carousel;

	// 当登录超时后，在内嵌的页面访问后端会跳转到登陆页面，但只是内嵌的页面跳转登录，需要父页面跳转
	var parentLocation = parent.location + "";
	if(parentLocation.indexOf("/adminLogin") == -1) {
		parent.location = "/adminLogin";
		return false;
	}

	refCode();

	// 背景图片轮播
	carousel.render({
		elem: '#login_carousel',
		width: '100%',
		height: '100%',
		interval: 3000,
		arrow: 'none',
		anim: 'fade',
		indicator: 'none'
	});

	// 点击刷新验证码
	$("#refCode_login_img").on("click", function() {
		refCode();
	});

	// 获取验证码
	function refCode() {
		$.ajax({
			url: "/secadminuser/getPictureVerificationCode",
			success: function(result) {
				$("#refCode_login_img").prop("src",
					"data:image/jpg;base64," + result.image);
				$("#adminCode").val("");
			}
		});
	}

	// 自定义验证规则
	form.verify({
		adminName: function(value) {
			var regUserName = /^[0-9a-zA-Z]{3,10}$/;
			if(!regUserName.test(value.trim())) {
				return "请输入正确的用户名";
			}
		},
		adminCode: function(value) {
			var regCode = /^[a-zA-Z]{6}$/;
			if(!regCode.test(value.trim())) {
				return "验证码输入有误";
			}
		}
	});

	// 监听提交
	form.on("submit(adminlogin)", function() {
		$.ajax({
			url: "/secadminuser/login",
			type: "post",
			data: {
				"code": $("#adminCode").val(),
				"adminName": $("#adminName").val(),
				"adminPassword": $("#adminPassword").val()
			},
			success: function(result) {
				if(result.status == 0) {
					location = "/";
				} else {
					refCode();
					$("#adminPassword").val("");
					layer.msg(result.msg);
				}
			}
		});
		return false;
	});
}();